//
//  CarManager.m
//  Auction
//
//  Created by Gary on 15/7/18.
//  Copyright (c) 2015年 Lebo. All rights reserved.
//

#import "CarManager.h"
#import "DataConverter.h"
#import "Common.h"
#import "FileIO.h"
#import "BrandModel.h"
#import "BrandSetModel.h"

@implementation CarManager

- (instancetype)init {
    self = [super init];
    if (self) {
        self.moduleID = ECarManager;
    }
    return self;
}

#pragma mark - 业务请求处理

//车辆品牌

- (NSArray*)getBrandListFromDB {
    NSArray* retArr = nil;
    [BF callBusinessFunc:BusinesID(EDatabaseManager, EDBSelectAll) inParam:[[BrandModel alloc] init] outParam:&retArr];
    NSMutableArray *arr = [NSMutableArray array];
    for (char letter = 'A'; letter <= 'Z'; letter++) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.letter == [cd] %@", [NSString stringWithFormat:@"%c", letter]];
        NSArray *results = [retArr filteredArrayUsingPredicate:predicate];
        if (results.count > 0) {
            [arr addObject:results];
        }
        //LogDebug(@"results: %@", results);
    }
    return arr;
}

//获取每个字母

static bool isHaveGotBrandList = false;

- (void)getBrandListWithData:(id)data {
    LogFunc;
    if (!isHaveGotBrandList) {
        [BF callBusinessFunc:BusinesID(ENetworkManager, ENetReqPostGetBrandList) inParam:[NSDictionary dictionaryWithObject:[NSMutableDictionary dictionary] forKey:kNetworkRequestData]];
        isHaveGotBrandList = true;
    }
}

- (void)getBrandListResponse:(id)data {
    LogFunc;
    NSDictionary *rspDic = data;
    id rspData = [rspDic objectForKey:kNetworkResponseData];
    if (![rspData isKindOfClass:[NSError class]]) {
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:rspData
                                                            options:NSJSONReadingMutableLeaves
                                                              error:&error];
        if (!error) {
            LogDebug(@"response dic: %@", dic);
            
            //将服务器返回的品牌转变为数据库的模型
            NSArray *brandList = [dic objectForKey:kNetworkResponseRetdataKey];
            NSMutableArray *arr = [NSMutableArray array];
            for (NSDictionary *dic in brandList) {
                BrandModel *model = [DataConverter ConvertBrandDicToModel:dic];
                if (model) {
                    [arr addObject:model];
                }
            }
            
            if (arr.count > 0) {
                //删除原先的数据
                NSNumber *ret = nil;
                [BF callBusinessFunc:BusinesID(EDatabaseManager, EDBDelete) inParam:[[BrandModel alloc] init] outParam:&ret];
                if (ret && ret.boolValue) {
                    ret = nil;
                    [BF callBusinessFunc:BusinesID(EDatabaseManager, EDBInsertBatch) inParam:arr outParam:&ret];
                    if (!(ret && ret.boolValue)) {
                        LogError(@"insert record into brand table failed: %@", [[BrandModel class] tableName]);
                    }
                } else {
                    LogError(@"delete brand table failed: %@", [[BrandModel class] tableName]);
                }
            }
            
        }
    }
}

#pragma mark - 业务框架函数

- (int)callBusinessFunc:(NSUInteger)funcID inParam:(id)inParam outParam:(id*)outParam {
    switch (funcID) {
        case ECarGetBrandListFromDB: {
            *outParam = [self getBrandListFromDB];
            break;
        }
            
        case ECarGetBrandList: {
            [self getBrandListWithData:inParam];
            break;
        }
            
            
        default: {
            break;
        }
    }
    
    return 0;
}

- (void)onBusinessNotify:(NSUInteger)notifyID inParam:(id)inParam {
    switch (notifyID) {
        case BusinesID(ENetworkManager, ENetReqPostGetBrandList): {
            if (inParam) {
                [self getBrandListResponse:inParam];
            }
            break;
        }
            
        default:
            break;
    }
}

@end
